function getValue() {
    var getpro = document.getElementById("province").value;
    var getcity = document.getElementById("city").value;
    var getarea = document.getElementById("area").value;
    alert(getpro + " " + getcity + " " + getarea);
}

function showForm() {
    document.getElementById("overlay").style.display = "flex";
}

function closeForm() {
    document.getElementById("overlay").style.display = "none";
}

document.addEventListener("DOMContentLoaded", function () {
    // 获取表单和按钮元素
    const form = document.getElementById("addressForm");
    const submitButton = form.querySelector("input[type='submit']");

    // 添加事件监听器到每个输入字段
    const inputs = form.querySelectorAll("input[type='text'], select");
    inputs.forEach(input => {
        input.addEventListener("input", validateForm);
    });

    // 验证表单字段
    function validateForm() {
        let allFilled = true;

        // 检查每个输入字段是否为空
        inputs.forEach(input => {
            if (input.type === "text" && !input.value.trim()) {
                allFilled = false;
            }
            if (input.tagName === "SELECT" && !input.value) {
                allFilled = false;
            }
        });

        // 根据验证结果更新按钮的背景颜色
        if (allFilled) {
            submitButton.style.backgroundColor = "#ff6d6d";
            submitButton.disabled = false;
        } else {
            submitButton.style.backgroundColor = "#cccccc"; // 灰色
            submitButton.disabled = true; // 禁用按钮
        }
    }
    // 初始验证
    validateForm();
    $(":submit").click(function () {
        addAddress();
    })
    function addAddress() {
        var name = $("input[name=username]").val();
        var province = $("select[name=province]").val();
        var city = $("select[name=city]").val();
        var county = $("select[name=county]").val();
        var street = $("input[name=address]").val();
        var phone = $("input[name=phone]").val();
        $.ajax({
            url: "addAddressServlet",
            type: "post",
            dataType: "text",
            data: {"name": name, "province": province, "city": city, "county": county, "street": street, "phone": phone},

            success: function (rows) {
                if (rows!=0){
                    location.reload();
                }
            }
        });
    }
    $(function () {
        $.ajax({
            url: "findAddressServlet",
            dataType: "json",
            type: "post",
            success: function (addresses) {
                for (var i = 0; i < addresses.length; i++) {
                    var name = addresses[i].name;
                    var phone = addresses[i].phone;
                    var province = addresses[i].province;
                    var city = addresses[i].city;
                    var county = addresses[i].county;
                    var street = addresses[i].street;
                    var isDefault = addresses[i].isDefault;
                    // 根据 isDefault 值来设置 "默认" 文字
                    var defaultText = isDefault == "1" ? "默认" : "";

                    var $div = $("<div class='information'>" +
                        "<p>" + name + "<span>" + defaultText + "</span></p>" +
                        "<p>" + phone + "</p>" +
                        "<p>" + province + city + county + street + "</p>" +
                        "<div>" +
                        "<a href='setDefaultServlet?id=" + addresses[i].id + "'>设为默认</a>" +
                        "<a href='updateFindAddressServlet?id=" + addresses[i].id + "'>编辑</a>" +
                        "</div>" +
                        "</div>");
                    $(".address").append($div);
                }
            }
        });
    });

});
